﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Security.Cryptography;
using System.Web.Security;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace Bai2
{
    public class Support
    {
        Random rand = new Random();
        public string getMD5(string clearText)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(clearText, "MD5");
        }
        public string getSHA1(string clearText)
        {
            return FormsAuthentication.HashPasswordForStoringInConfigFile(clearText, "SHA1");
        }
        public string getMd5Hash(string input)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            MD5 md5Hasher = MD5.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString().ToUpper();
        }
        public string getSha1Hash(string input)
        {
            // Create a new instance of the MD5CryptoServiceProvider object.
            SHA1 sha1Hasher = SHA1.Create();

            // Convert the input string to a byte array and compute the hash.
            byte[] data = sha1Hasher.ComputeHash(Encoding.Default.GetBytes(input));

            // Create a new Stringbuilder to collect the bytes
            // and create a string.
            StringBuilder sBuilder = new StringBuilder();

            // Loop through each byte of the hashed data 
            // and format each one as a hexadecimal string.
            for (int i = 0; i < data.Length; i++)
            {
                sBuilder.Append(data[i].ToString("x2"));
            }

            // Return the hexadecimal string.
            return sBuilder.ToString().ToUpper();
        }

        public string[] GetFiles(string path)
        {
            return Directory.GetFiles(path);
        }

        //image to byte[]
        public byte[] Image2Byte(string text)
        {
            string[] imgs = GetFiles(Path.GetFullPath(Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "App_LocalResources")));
            int x = rand.Next(imgs.Length);
            try
            {
                Image img = Image.FromFile(imgs[x].ToString());
                SolidBrush solid = new SolidBrush(Color.Green);
                Font font = new Font("Tahoma", 14);
                MemoryStream ms = new MemoryStream();

                Graphics g = Graphics.FromImage(img);
                g.DrawString(text, font, solid, new PointF(0, img.Height / 2));
                
                img.Save(ms, ImageFormat.Jpeg);
                ms.Dispose();
                ms.Close();
                return ms.ToArray();
            }
            catch (Exception)
            {
                return null;
            }
        }

        public byte[] Image2Byte2(string text, string pathFileName)
        {
            try
            {
                Image img = Image.FromFile(pathFileName);
                SolidBrush solid = new SolidBrush(Color.Green);
                Font font = new Font("Tahoma", 14);
                MemoryStream ms = new MemoryStream();

                Graphics g = Graphics.FromImage(img);
                g.DrawString(text, font, solid, new PointF(0, img.Height / 2));

                img.Save(ms, ImageFormat.Jpeg);
                ms.Dispose();
                ms.Close();
                return ms.ToArray();
            }
            catch (Exception)
            {
                return null;
            }
        }

        //drawing

        
    }
}